package com.ruiysoft.uppic;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectResult;
import com.ruiysoft.controller.upload.pic.ALiOSSController;
import com.ruiysoft.util.Md5Util;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class ALiOSStest {
    private static final Logger log = LoggerFactory.getLogger(ALiOSSController.class);

    // Endpoint以杭州为例，其它Region请按实际情况填写。
    private String endpoint = "http://oss-cn-shenzhen.aliyuncs.com";
    // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
    private String accessKeyId = "LTAIOlwbYUgNYUBv";
    private String accessKeySecret = "oiItP7Mhorhe2FCg9WoQ7vRoXe7PeR";

    // 创建OSSClient实例。
//    private OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);


    //String localFilePath = "/Users/liudh/Desktop/WechatIMG383.jpeg";
    //默认不指定key的情况下，以文件内容的hash值作为文件名
    String key = null;

    @Test
    public void upload() {

        log.debug("upload api is invoked..");
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        Map<String, Object> map = new HashMap<String, Object>();
        String aa = "aaaaaaaaaaaa";
        String key = Md5Util.MD5(aa);
        map.put("success", false);
        try {
//            String upToken = auth.uploadToken(bucket);
//            Response response = uploadManager.put(file.getInputStream(), key, upToken,null,null);
            PutObjectResult result = ossClient.putObject("xianxiuyigou", key, new ByteArrayInputStream(aa.getBytes()));

            map.put("success", true);
//            map.put("downloadPath", "http://media.avicyt.com/" + putRet.key);
            /*

             * Download an object from your bucket

             */

            System.out.println("Downloading an object");

            OSSObject object = ossClient.getObject(new GetObjectRequest("xianxiuyigou", key));

            System.out.println("Content-Type: "  + object.getObjectMetadata().getContentType());

            displayTextInputStream(object.getObjectContent());
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message: " + oe.getErrorCode());
            System.out.println("Error Code:       " + oe.getErrorCode());
            System.out.println("Request ID:      " + oe.getRequestId());
            System.out.println("Host ID:           " + oe.getHostId());
            map.put("errorMsg", oe.getMessage());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message: " + ce.getMessage());
            map.put("errorMsg", ce.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            /*

             * Do not forget to shut down the client finally to release all allocated resources.

             */

            ossClient.shutdown();
        }
    }
    private static void displayTextInputStream(InputStream input) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        while (true) {
            String line = reader.readLine();
            if (line == null) break;
            System.out.println("\t" + line);
        }
        System.out.println();
        reader.close();
    }
}
